/* 
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research 
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */

/*
 * Created on Mar 5, 2005
 */
package edu.mit.csail.relo.agent;

import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.ui.parts.AbstractEditPartViewer;
import org.eclipse.jface.action.MenuManager;
import org.openrdf.model.Resource;

import edu.mit.csail.relo.ReloPlugin;
import edu.mit.csail.relo.agent.AgentManager.ViewAgent;
import edu.mit.csail.relo.modelBridge.Artifact;
import edu.mit.csail.relo.parts.AbstractReloEditPart;
import edu.mit.csail.relo.store.ReloRdfRepository;
import edu.mit.csail.relo.store.StoreUnionRepository;

/**
 * @author vineet
 *
 */
public abstract class BrowseModel {
	static final Logger logger = ReloPlugin.getLogger(BrowseModel.class);
    public static List<BrowseModel> models = new LinkedList<BrowseModel>();
    private EditPartFactory editPartFactory = null; 
    public List<ViewAgent> agents = new LinkedList<ViewAgent>();


    /**
     * Needed for writing and instantiating to store
     */
    public abstract String getBundleSymbolicName();

    public BrowseModel() {
        models.add(this);
		// add system agents (these need to not be shown in the context menu)
		//agents.add(DebugModeAgent.singleton);	// debug agent (for i/f to change flag)
        agents.add(new ScriptManager());
    }
    
    /**
     * Called by agents (in their constructor) that need to be initialized by other sources.
     * 
     * Note: New browseModels will not connect to such an agent automatically
     *  
     * @param agent
     */
    public static final void connectToAllModels(ViewAgent agent) {
        for (BrowseModel bm : BrowseModel.models) {
            bm.agents.add(agent);
        }
    }

    public void init(AbstractEditPartViewer viewer) {
        editPartFactory = createEditPartFactory(viewer);
		viewer.setEditPartFactory(editPartFactory);
    }
    

    private StoreUnionRepository reloRdfRepository;
    public void setRepo(ReloRdfRepository rdfModel) {
    	if(reloRdfRepository==null) {
    		reloRdfRepository = new StoreUnionRepository(rdfModel);
    	} else {
    		reloRdfRepository.setStoreRepo(rdfModel);
    	}
    }
    public ReloRdfRepository getRepo() {
        return reloRdfRepository;
    }
    

	public boolean artifactLoadable(Artifact art, org.openrdf.model.Resource artType) {
	    return true;
	}

	/**
	 * Allows conversion of Java objects (provided by external frameworks) to Artifacts
	 * 
	 * An Artifact is the representation of the model, it is basically an RDF resource
	 * with additional helper methods
	 * 
	 * @param src
	 * @return
	 */
	public Artifact getArtifact(Object src) {
        logger.debug("Unexpected source type for getArtifact");
        // edge cases, just in case
	    if (src instanceof Resource)
	        return new Artifact((Resource) src);
	    else if (src instanceof Artifact)
	        return (Artifact) src;
	    return null;
	}

	public Object initializePart(AbstractReloEditPart part) {
		return AgentManager.initializeAgents(part, agents);
	}

	public void cleanPart(AbstractReloEditPart part, Object agentManagerToken) {
		AgentManager.cleanAgents(part, agentManagerToken);
	}

	public void buildModelContextMenu(
			MenuManager subMenu, 
			String groupName,
			AbstractReloEditPart part, 
			Object agentManagerToken) {
		AgentManager.buildAgentsContextMenu(subMenu, groupName, part, agentManagerToken);
	}


    /**
     * @return Returns the editPartFactory.
     */
    public EditPartFactory getEditPartFactory() {
        return editPartFactory;
    }

    /**
     * @param editPartFactory The editPartFactory to set.
     */
    public void setEditPartFactory(EditPartFactory editPartFactory) {
        this.editPartFactory = editPartFactory;
    }

    protected abstract EditPartFactory createEditPartFactory(AbstractEditPartViewer viewer);

    
}
